package com.example.company

import android.view.View
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.common.view.view.BaseMvvmActivity
import com.example.company.adapter.MerChantAdapter
import com.example.company.databinding.ActivityApproveBinding
import com.example.company.vm.CompanyViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = "/company/ApproveActivity")
class ApproveActivity : BaseMvvmActivity<ActivityApproveBinding>() {
    var name:String = "新建"
    private val approveViewModel:CompanyViewModel by viewModels()
    override fun initView() {
        //返回
        mDataBinding.ivBack.setOnClickListener {
            finish()
        }
        mDataBinding.rg.setOnCheckedChangeListener { group, checkedId ->
            when(checkedId){
                R.id.rb1->{
                    mDataBinding.rb1.isChecked = true
                    mDataBinding.rb2.isChecked = false
                }
                R.id.rb2->{
                    mDataBinding.rb1.isChecked = false
                    mDataBinding.rb2.isChecked = true
                }
            }
            if (mDataBinding.rb1.isChecked){
                name = "已审核"
            }else{
                name = "驳回"
            }
            approveViewModel.getMerChant()
        }
        approveViewModel.getMerChant()
        approveViewModel.mGetMerChantSuccess.observe(this){ item->
            //根据name的值决定是否过滤
            val filterList = if (name == "驳回"){
                item.filter { it.status == "驳回" }//只保留状态为驳回的选项
            }else{
                item.filter { it.status == "已审核" }//只保留状态为已审核的选项
            }
            //发数据
            mDataBinding.rv.run {
                layoutManager = LinearLayoutManager(this@ApproveActivity)
                adapter = MerChantAdapter(filterList).apply {
                    notifyDataSetChanged()
                    setItemOnClickListener(object :MerChantAdapter.ItemOnClickListener{
                        override fun onClick(view: View, position: Int) {
                            ARouter.getInstance().build("/company/CompanyDetailActivity")
                                .withInt("merChantId",filterList[position].id)
                                .navigation()
                        }
                    })
                }
            }
            approveViewModel.mError.observe(this){
                ToastUtils.showLong(it)
            }
        }
    }

    override fun getLayoutId(): Int = R.layout.activity_approve

}